
#include "gtest/gtest.h"
#include <mockcpp/mokc.h>
#include <mockcpp/mockcpp.hpp>

#include <vector>
#include <iostream>

#include "topoinfo_struct.h"
#include "log.h"
#include "checker.h"
#include "virtual_topo.h"
#include "testcase_utils.h"

using namespace hccl;

class BatchSendRecvTest : public testing::Test {
protected:
    static void SetUpTestCase()
    {
        std::cout << "BatchSendRecvTest set up." << std::endl;
    }

    static void TearDownTestCase()
    {
        std::cout << "BatchSendRecvTest tear down." << std::endl;
    }

    virtual void SetUp()
    {
        const ::testing::TestInfo* const test_info = ::testing::UnitTest::GetInstance()->current_test_info();
        std::string caseName = "analysis_result_" + std::string(test_info->test_case_name()) + "_" + std::string(test_info->name());
        Checker::SetDumpFileName(caseName);
    }

    virtual void TearDown()
    {
        Checker::SetDumpFileName("analysis_result");
        GlobalMockObject::verify();
        // 这边每个case执行完成需要清理所有的环境变量，如果有新增的环境变量，需要在这个函数中进行清理
        ClearHcclEnv();
    }
};

TEST_F(BatchSendRecvTest, batch_send_recv_allsendrecv_intrapod_2ranks)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 2, 1);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
    testOpParam.tag = "batchsendrecv";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 1024;
    testOpParam.DataDes.dataType = HCCL_DATA_TYPE_INT8;
    testOpParam.allRanksSendRecvInfoVec.resize(ranktable.rankNum);
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(BatchSendRecvTest, batch_send_recv_allsendrecv_intrapod_8ranks)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
    testOpParam.tag = "batchsendrecv";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 1024;
    testOpParam.DataDes.dataType = HCCL_DATA_TYPE_INT8;
    testOpParam.allRanksSendRecvInfoVec.resize(ranktable.rankNum);
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(BatchSendRecvTest, batch_send_recv_allsendrecv_single_rank)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 1);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
    testOpParam.tag = "batchsendrecv";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 1024;
    testOpParam.DataDes.dataType = HCCL_DATA_TYPE_INT8;
    testOpParam.allRanksSendRecvInfoVec.resize(ranktable.rankNum);
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(BatchSendRecvTest, batch_send_recv_allsendrecv_interpod_48ranks)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 4, 2, 8);
    gen.GenRankTable(topoMeta, ranktable);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
    testOpParam.tag = "batchsendrecv";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 1024 * 1024;
    testOpParam.DataDes.dataType = HCCL_DATA_TYPE_INT8;
    testOpParam.allRanksSendRecvInfoVec.resize(ranktable.rankNum);
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}

TEST_F(BatchSendRecvTest, batch_send_recv_allsendrecv_intrapod_8ranks_datasize_exceed_cclbuffersize)
{
    RankTable_t ranktable;
    RankTable_For_LLT gen;
    TopoMeta topoMeta;
    gen.GenTopoMeta(topoMeta, 1, 1, 8);
    gen.GenRankTable(topoMeta, ranktable);
    setenv("HCCL_BUFFSIZE", "1", 1);

    TestOpParam testOpParam;
    testOpParam.opType = HcclCMDType::HCCL_CMD_BATCH_SEND_RECV;
    testOpParam.tag = "batchsendrecv";
    testOpParam.opMode = OPBASE;
    testOpParam.devtype = DevType::DEV_TYPE_910B;
    testOpParam.DataDes.count = 1024 * 1024 * 10 + 1;
    testOpParam.DataDes.dataType = HCCL_DATA_TYPE_INT8;
    testOpParam.allRanksSendRecvInfoVec.resize(ranktable.rankNum);
    Checker checker;
    HcclResult ret;
    ret = checker.Check(testOpParam, ranktable, topoMeta);

    EXPECT_EQ(ret, HcclResult::HCCL_SUCCESS);
}